<template>
  <div class="add-form">
    <el-dialog :title="cTitle" :visible.sync="dialogFormVisible">
      <el-form
        :rules="rules"
        ref="refTags"
        :model="tag"
        label-position="left"
        label-width="150px"
        style="width: 80%; margin-left:10px"
      >
        <el-form-item v-if="this.$route.query.id ? false : true" label="所属学科" prop="subjectID">
          <el-select v-model="tag.subjectID" placeholder="请选择频道" >
            <el-option
              v-for="item in subjList"
              :key="item.id"
              :label="item.subjectName"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="tagName">
          <el-input v-model="tag.tagName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogClose">{{$t('table.cancel')}}</el-button>
        <el-button type="primary" @click="hConfirm">{{$t('table.confirm')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { list } from '@/api/hmmm/subjects'
import { update, add } from '@/api/hmmm/tags.js'
export default {
  name: 'subjects-tagsAdd',
  props: {
    // 接收的标签信息
    tag: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      subId: '',
      dialogFormVisible: false,
      subjList: [],
      // 表单验证
      rules: {
        tagName: [
          { required: true, message: '目录名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    cTitle () {
      return this.tag.id ? '编辑' : '新增'
    }
  },
  methods: {
    // 弹层显示
    dialogShow () {
      this.dialogFormVisible = true
    },
    // 弹层隐藏
    dialogClose () {
      this.dialogFormVisible = false
    },
    // 表单提交 新增或者是编辑
    hConfirm () {
      this.$refs.refTags.validate(async valid => {
        if (this.$route.query.id) {
          this.tag.subjectID = Number(this.$route.query.id)
        }
        this.doConfirm()
      })
    },
    async doConfirm () {
      try {
        // 如果是编辑
        if(this.tag.id) {
          const result = await update(this.tag)
          console.log(result)
          this.$emit('modifyOK')
        } else {
          // 如果是新增
           const result = await add(this.tag)
          console.log(result)
          this.$emit('addOK')
        } 
        this.dialogClose()
      } catch (error) {
        console.log(error)
      }
    },
    // 获取学科列表
    async getSubjList () {
      try {
        const { data: res } = await list()
        console.log(res)
        this.subjList = res.items
      } catch (err) {
        console.log(err)
      }
    }
  },
  // 挂载结束

  mounted: function () {},
  // 创建完毕状态
  created () {
    if (this.$route.query.id == null) {
      this.getSubjList()
    }
  },
  // 组件更新
  updated: function () {}
}
</script>
<style>
.el-form--label-left .el-form-item__label {
  text-align: right;
}
.el-form-item--medium {
  margin-bottom: 22px;
}
.el-dialog__footer {
  text-align: center;
}
</style>
